package com.zrrd.yunchmall.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.yunchmall.user.entity.Role;
import com.zrrd.yunchmall.user.service.IRoleService;
import com.zrrd.yunchmall.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 后台用户角色表 前端控制器
 * </p>
 *
 * @author LYY
 * @since 2024-01-15
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("/role")
@Api(tags = "权限服务-角色模块")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @ApiOperation("查询所有角色信息")
    @GetMapping("/listAll")
    public ResponseResult listAll(){
        return new ResponseResult(200,"查询成功", roleService.list());
    }
    @ApiOperation("查询角色列表")
    @GetMapping("/list")
    public ResponseResult select(@RequestParam int pageSize,@RequestParam int pageNum,String keyword){
        QueryWrapper queryWrapper=new QueryWrapper<>();
        if (!StringUtils.isEmpty(keyword)){
            queryWrapper.like("name",keyword);
        }
        return new ResponseResult(200,"查询成功", roleService.page(new Page<>(pageNum,pageSize),queryWrapper));
    }
    @ApiOperation("查询角色菜单")
    @GetMapping("/listMenu/{id}")
    public ResponseResult Menu(@PathVariable("id") int id){
        return new ResponseResult(200,"查询成功", roleService.getById(id));
    }

    @ApiOperation("是否启用该角色")
    @PostMapping("/updateStatus/{id}")
    public ResponseResult updateStatus(@PathVariable int id,@RequestParam int status){
        UpdateWrapper updateWrapper=new UpdateWrapper<>();
        updateWrapper.set("status",status);
        updateWrapper.in("id",id);
        roleService.update(updateWrapper);
        return new ResponseResult(200,"修改成功");
    }
    @ApiOperation("编辑角色")
    @PostMapping("/update/{id}")
    public ResponseResult update(@PathVariable int id, @RequestBody Role role){
        UpdateWrapper updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        roleService.update(role,updateWrapper);
        return new ResponseResult(200,"修改成功");
    }
    @ApiOperation("删除角色")
    @PostMapping("/delete")
    public ResponseResult update(@RequestParam String ids){
        for (String id:ids.split(",")) {
            roleService.removeById(Long.valueOf(id));
        }
        return new ResponseResult(200,"删除成功!");
    }
    @ApiOperation("编辑角色")
    @PostMapping("/create")
    public ResponseResult create(@RequestBody Role role){
        QueryWrapper queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name",role.getName());
        if (roleService.count(queryWrapper)>0){
            return new ResponseResult(205,"注册失败:角色名已存在");
        }
        roleService.save(role);
        return new ResponseResult(200,"修改成功",role);
    }

    @GetMapping("/listResource/{id}")
    @ApiOperation("查询角色资源")
    public ResponseResult listResource(@PathVariable("id") Integer id){
        QueryWrapper queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",id);
        return new ResponseResult<>(200,"查询成功",roleService.getOne(queryWrapper));
    }

            //未写完,分配资源未写完
    @ApiOperation("分配角色菜单")
    @PostMapping("/allocMenu")
    public ResponseResult allocMenu(@RequestParam int roleId,@RequestParam String menuIds){
        for (String menuId:menuIds.split(",")) {
            UpdateWrapper updateWrapper=new UpdateWrapper<>();
            updateWrapper.eq("id",roleId);
            updateWrapper.clear();
            updateWrapper.set("menu_id",menuId);
            updateWrapper.in("role_id",roleId);
            roleService.update(updateWrapper);
        }
        return new ResponseResult(200,"修改成功");
    }









}
